<?php
// +----------------------------------------------------------------------
// | 文件: index.php
// +----------------------------------------------------------------------
// | 功能: 提供todo api接口
// +----------------------------------------------------------------------
// | 时间: 2021-11-15 16:20
// +----------------------------------------------------------------------
// | 作者: rangangwei<gangweiran@tencent.com>
// +----------------------------------------------------------------------

namespace app\controller;

use Error;
use Exception;
use app\model\Emojidb;
use think\response\Html;
use think\response\Json;
use think\facade\Log;

class Emoji
{

    /**
     * 主页静态页面
     * @return Html
     */
    public function index(): Html
    {
    }

    public function pageSize(): Json
    {
        return json([
            'total' => 639
        ]);
    }

    public function addEmoji(): Json
    {
        try {
            $name = $_GET['name'];
            $url = $_GET['url'];
            $type = $_GET['type'];

            $emoji_db = new Emojidb;
            #$ret = $emoji_db->create(
            #    //["id" => 1, "name" => $name, "url" => $url, "type" => $type, "createTime" => "2022-01-01 00:00:00"],
            #    ["id" => 1, "name" => "23wwqewqe", "url" => "ewqewqe", "type" => "ewqewqe", "createTime" => "2022-01-01 00:00:00"],
            #    ['id', 'name', 'url', 'createTime', 'type'],
            #    true
            #);
            $emoji_db->insertAll(["name" => "23wwqewqe", "url" => "ewqewqe", "type" => "ewqewqe", "createTime" => "2022-01-01 00:00:00"]);
            return json(["code" => 0]);
        } catch (Error $e) {
            $res = [
                "code" => -1,
                "data" => json($_GET),
                "errorMsg" => ("写入异常" . $e->getMessage())
            ];
            return json($res);
        }
    }

    /**
     * 获取todo list
     * @return Json
     */
    public function getOneItem(): Json
    {
        try {
            $data = (new Emojidb)->find(1);
            if ($data == null) {
                $item = [];
            }else {
                $item = $data;
            }
            $res = [
                "code" => 0,
                "data" =>  $data
            ];
            Log::write('getOneItem rsp: '.json_encode($res));
            return json($res);
        } catch (Error $e) {
            $res = [
                "code" => -1,
                "data" => [],
                "errorMsg" => ("查询异常" . $e->getMessage())
            ];
            Log::write('getOneItem rsp: '.json_encode($res));
            return json($res);
        }
    }

}
